
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Timsort排序算法 · tgor note</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="tgor">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="插入排序分析与Java实现.html" />
    
    
    <link rel="prev" href="Java注解.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Me
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../category/微服务.html">
            
                <a href="../category/微服务.html">
            
                    
                    微服务
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../Java-Spring/Feign.html">
            
                <a href="../Java-Spring/Feign.html">
            
                    
                    HTTP客户端Feign
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../Java-其他库/限流.html">
            
                <a href="../Java-其他库/限流.html">
            
                    
                    限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    限流Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../Java-Spring/负载均衡.html">
            
                <a href="../Java-Spring/负载均衡.html">
            
                    
                    负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    负载均衡Nginx
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../Java-其他库/熔断Hystrix.html">
            
                <a href="../Java-其他库/熔断Hystrix.html">
            
                    
                    熔断Hystrix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html">
            
                    
                    缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../Java-其他库/缓存.html">
            
                <a href="../Java-其他库/缓存.html#本地缓存">
            
                    
                    本地缓存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    分布式缓存Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html">
            
                    
                    分布式事务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../Java-其他库/分布式事务.html">
            
                <a href="../Java-其他库/分布式事务.html#事务消息">
            
                    
                    可靠事务消息
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../Java-Spring/业务监控.html">
            
                <a href="../Java-Spring/业务监控.html">
            
                    
                    业务监控
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.13" data-path="../Java-其他库/分布式定时作业.html">
            
                <a href="../Java-其他库/分布式定时作业.html">
            
                    
                    分布式作业调度ElasticJob
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.14" data-path="../Java-ORM/ORM.html">
            
                <a href="../Java-ORM/ORM.html">
            
                    
                    ORM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.15" data-path="../Java-其他库/分布式系统及CAP原理.html">
            
                <a href="../Java-其他库/分布式系统及CAP原理.html">
            
                    
                    分布式系统及CAP原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.16" data-path="../MQ/RabbitMQ笔记.html">
            
                <a href="../MQ/RabbitMQ笔记.html">
            
                    
                    消息驱动Rabbitmq
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../category/数据库.html">
            
                <a href="../category/数据库.html">
            
                    
                    数据库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../Mysql/经典数据库设计案例.html">
            
                <a href="../Mysql/经典数据库设计案例.html">
            
                    
                    mysql设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../Mysql/Mysql事务与锁.html">
            
                <a href="../Mysql/Mysql事务与锁.html">
            
                    
                    mysql事务与锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../Mysql/mysql数据类型.html">
            
                <a href="../Mysql/mysql数据类型.html">
            
                    
                    mysql数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../Mysql/mysql索引及查询.html">
            
                <a href="../Mysql/mysql索引及查询.html">
            
                    
                    mysql索引与查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../Mysql/mysql索引及查询.html">
            
                <a href="../Mysql/mysql索引及查询.html#查询语句设计准则">
            
                    
                    mysql查询语句设计准则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../Mysql/mysql性能测试.html">
            
                <a href="../Mysql/mysql性能测试.html">
            
                    
                    mysql性能测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="../Mysql/mysql主从同步与读写分离.html">
            
                <a href="../Mysql/mysql主从同步与读写分离.html">
            
                    
                    mysql主从同步与读写分离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                <a href="../Java-Spring/SpringBoot事务注解@Transactional.html">
            
                    
                    Spring事务注解@Transactional
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="../数据结构与算法/位运算应用实例.html">
            
                <a href="../数据结构与算法/位运算应用实例.html">
            
                    
                    位运算应用实例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.10" data-path="../Mongodb-Client/morphia-API汇总.html">
            
                <a href="../Mongodb-Client/morphia-API汇总.html">
            
                    
                    morphia API汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.11" data-path="../Mongodb-Client/mongo驱动连接.html">
            
                <a href="../Mongodb-Client/mongo驱动连接.html">
            
                    
                    mongodb驱动与连接
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.12" data-path="../Mongodb/mongodb-ObjectId实践.html">
            
                <a href="../Mongodb/mongodb-ObjectId实践.html">
            
                    
                    mongodb ObjectId实践
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.13" data-path="../Mongodb/mongodb角色权限.html">
            
                <a href="../Mongodb/mongodb角色权限.html">
            
                    
                    mongodb角色权限
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../category/java并发.html">
            
                <a href="../category/java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="Java并发.html">
            
                <a href="Java并发.html">
            
                    
                    Java并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="Java并发.html">
            
                <a href="Java并发.html#锁">
            
                    
                    Java并发之锁设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="Java并发.html">
            
                <a href="Java并发.html#synchronized">
            
                    
                    Java并发之synchronized
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Java之Lock.html">
            
                <a href="Java之Lock.html#reentrantlock">
            
                    
                    Java Lock之ReentrantLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="Java之Lock.html">
            
                <a href="Java之Lock.html#reentrantreadwritelock">
            
                    
                    Java Lock之ReentrantReadWriteLock
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="Java之Future.html">
            
                <a href="Java之Future.html">
            
                    
                    Java Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="Java之Future.html">
            
                <a href="Java之Future.html#futuretask">
            
                    
                    Java Future之FutureTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="Java之Future.html">
            
                <a href="Java之Future.html#completablefuture">
            
                    
                    Java Future之CompletableFuture
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="Java之Future.html">
            
                <a href="Java之Future.html#forkjointask">
            
                    
                    Java Future之ForkJoinTask
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="Java之Thread.html">
            
                <a href="Java之Thread.html">
            
                    
                    Java Thread
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../category/redis.html">
            
                <a href="../category/redis.html">
            
                    
                    Redis
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html">
            
                    
                    Redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#集群">
            
                    
                    Redis集群
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#消息订阅">
            
                    
                    Redis消息订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#lua">
            
                    
                    Redis Lua脚本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Redis/Redis.html">
            
                <a href="../Redis/Redis.html#配置汇总">
            
                    
                    Redis配置汇总
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../category/运维.html">
            
                <a href="../category/运维.html">
            
                    
                    运维
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../运维/Linux常用操作汇总.html">
            
                <a href="../运维/Linux常用操作汇总.html">
            
                    
                    Linux常用操作汇总
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../运维/阿里云笔记.html">
            
                <a href="../运维/阿里云笔记.html">
            
                    
                    阿里云笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../category/nginx.html">
            
                <a href="../category/nginx.html">
            
                    
                    Nginx
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html">
            
                    
                    Nginx基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../运维/Nginx负载均衡.html">
            
                <a href="../运维/Nginx负载均衡.html">
            
                    
                    Nginx负载均衡
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../运维/Nginx限流.html">
            
                <a href="../运维/Nginx限流.html">
            
                    
                    Nginx限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../运维/Nginx学习笔记.html">
            
                <a href="../运维/Nginx学习笔记.html#六、其他">
            
                    
                    Nginx客户端辅助技巧
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../category/容器化.html">
            
                <a href="../category/容器化.html">
            
                    
                    容器化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html">
            
                    
                    Docker学习笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#基础操作">
            
                    
                    Docker基础操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../Docker/docker笔记.html">
            
                <a href="../Docker/docker笔记.html#dockerfile">
            
                    
                    DockerFile
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../category/Web.html">
            
                <a href="../category/Web.html">
            
                    
                    Web
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../运维/Http入坑Https笔记.html">
            
                <a href="../运维/Http入坑Https笔记.html">
            
                    
                    Http入坑Https笔记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../Web/阿里云OSS.html">
            
                <a href="../Web/阿里云OSS.html">
            
                    
                    OSS
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../category/java基础.html">
            
                <a href="../category/java基础.html">
            
                    
                    Java基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="Java基础.html">
            
                <a href="Java基础.html#jvm">
            
                    
                    JVM基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="Java基础.html">
            
                <a href="Java基础.html#语法糖">
            
                    
                    Java语法糖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="Java基础.html">
            
                <a href="Java基础.html#数据结构">
            
                    
                    Java数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="Java8函数式编程.html">
            
                <a href="Java8函数式编程.html">
            
                    
                    java8函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="ArrayList源码分析.html">
            
                <a href="ArrayList源码分析.html">
            
                    
                    JDK源码分析ArrayList
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="HashMap源码分析.html">
            
                <a href="HashMap源码分析.html">
            
                    
                    JDK源码分析HashMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="Java注解.html">
            
                <a href="Java注解.html">
            
                    
                    Java注解
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.10.8" data-path="Timsort排序算法.html">
            
                <a href="Timsort排序算法.html">
            
                    
                    Timsort排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="插入排序分析与Java实现.html">
            
                <a href="插入排序分析与Java实现.html">
            
                    
                    插入排序分析与Java实现
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../category/工具与规范.html">
            
                <a href="../category/工具与规范.html">
            
                    
                    工具与规范
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../工具/Maven使用技巧汇总.html">
            
                <a href="../工具/Maven使用技巧汇总.html">
            
                    
                    Maven使用技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../编码规范/Java编码规范.html">
            
                <a href="../编码规范/Java编码规范.html">
            
                    
                    Java编码规范
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../category/OLAP.html">
            
                <a href="../category/OLAP.html">
            
                    
                    OLAP
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../数据处理/storm学习笔记.html">
            
                <a href="../数据处理/storm学习笔记.html">
            
                    
                    流式计算Storm学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../category/数据运营.html">
            
                <a href="../category/数据运营.html">
            
                    
                    数据运营
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../数据运营/增长黑客数据运营.html">
            
                <a href="../数据运营/增长黑客数据运营.html">
            
                    
                    增长黑客-数据运营
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../category/前端技术栈.html">
            
                <a href="../category/前端技术栈.html">
            
                    
                    前端技术栈
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../前端/JavaScript设计模式.html">
            
                <a href="../前端/JavaScript设计模式.html">
            
                    
                    JavaScript设计模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../前端/html相对路径与绝对路径.html">
            
                <a href="../前端/html相对路径与绝对路径.html">
            
                    
                    Html相对路径与绝对路径
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../前端/微信小程序学习笔记.html">
            
                <a href="../前端/微信小程序学习笔记.html">
            
                    
                    微信小程序学习笔记
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../category/个人.html">
            
                <a href="../category/个人.html">
            
                    
                    个人
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../个人/简历.html">
            
                <a href="../个人/简历.html">
            
                    
                    简历
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >Timsort排序算法</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="timsort&#x6392;&#x5E8F;&#x7B97;&#x6CD5;">Timsort&#x6392;&#x5E8F;&#x7B97;&#x6CD5;</h1>
<p>&#x6807;&#x7B7E;&#xFF08;&#x7A7A;&#x683C;&#x5206;&#x9694;&#xFF09;&#xFF1A; Java-Base &#x6570;&#x636E;&#x7ED3;&#x6784;&#x4E0E;&#x7B97;&#x6CD5;</p>
<hr>
<h2 id="&#x7B97;&#x6CD5;&#x5B9E;&#x73B0;&#x539F;&#x7406;">&#x7B97;&#x6CD5;&#x5B9E;&#x73B0;&#x539F;&#x7406;</h2>
<p>TimSort&#x539F;&#x7406;&#xFF1A;&#x73B0;&#x5B9E;&#x6570;&#x636E;&#x901A;&#x5E38;&#x4F1A;&#x6709;&#x90E8;&#x5206;&#x662F;&#x5DF2;&#x7ECF;&#x6392;&#x597D;&#x5E8F;&#xFF0C;TimSort&#x6B63;&#x662F;&#x5229;&#x7528;&#x8FD9;&#x4E00;&#x70B9;&#xFF0C;&#x5C06;&#x6570;&#x7EC4;&#x62C6;&#x6210;&#x591A;&#x4E2A;&#x90E8;&#x5206;&#x5DF2;&#x6392;&#x5E8F;&#x7684;&#x5206;&#x533A;&#xFF0C;&#x90E8;&#x5206;&#x672A;&#x6392;&#x5E8F;&#x5206;&#x533A;&#x91CD;&#x65B0;&#x6392;&#x5E8F;&#xFF0C;&#x6700;&#x540E;&#x5C06;&#x591A;&#x4E2A;&#x5206;&#x533A;&#x5408;&#x5E76;&#x5E76;&#x6392;&#x5E8F;&#x3002;</p>
<p>&#x4F8B;&#x5982;&#xFF1A;array[] = <code>[24,63,70,55,41,92,81,80]</code>&#xFF0C;&#x6392;&#x5E8F;&#x6B65;&#x9AA4;&#x5982;&#x4E0B;&#xFF1A;</p>
<ol>
<li>&#x62C6;&#x5206;&#x5206;&#x533A;&#xFF1A;<code>[24,63]</code>, <code>[70,55]</code>, <code>[41,92]</code>, <code>[81,80]</code></li>
<li>&#x91CD;&#x6392;&#x5206;&#x533A;&#xFF1A;<code>[24,63]</code>, <code>[55,70]</code>, <code>[41,92]</code>, <code>[80,81]</code></li>
<li>&#x5408;&#x5E76;&#x5206;&#x533A;&#xFF1A;<code>[24,63,55,70]</code>, <code>[41,92,80,81]</code></li>
<li>&#x91CD;&#x6392;&#x5206;&#x533A;&#xFF1A;<code>[24,55,63,70]</code>, <code>[41,80,81,92]</code></li>
<li>&#x5408;&#x5E76;&#x5206;&#x533A;&#xFF1A;<code>[24,55,63,70,41,80,81,92]</code></li>
<li>&#x91CD;&#x6392;&#xFF1A;<code>[24,41,55,63,70,80,81,92]</code></li>
</ol>
<hr>
<h2 id="java-timsort&#x5B9E;&#x73B0;&#x539F;&#x7406;">Java Timsort&#x5B9E;&#x73B0;&#x539F;&#x7406;</h2>
<p>JDK1.8&#x4EE5;&#x540E;&#x9ED8;&#x8BA4;&#x91C7;&#x7528;Timsort&#x6392;&#x5E8F;&#xFF0C;&#x5B9E;&#x73B0;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-java">List list = <span class="hljs-keyword">new</span> ArrayList&lt;Integer&gt;();
...
Collections.sort(list, <span class="hljs-keyword">new</span> Comparator&lt;Integer&gt;() {
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">compare</span><span class="hljs-params">(Integer o1, Integer o2)</span> </span>{
        <span class="hljs-keyword">if</span> (o2 &gt; o1) {
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;
        }
    }
});
</code></pre>
<p>Java&#x5BF9;&#x4E8E;Timsort&#x7684;&#x5B9E;&#x73B0;&#x4E0E;&#x4E0A;&#x8FF0;&#x539F;&#x7406;&#x6709;&#x533A;&#x522B;&#x3002;Java&#x7248;&#x9996;&#x5148;&#x4F1A;&#x6839;&#x636E;&#x6570;&#x7EC4;&#x957F;&#x5EA6;&#xFF0C;&#x91C7;&#x7528;Binarysort&#xFF08;&#x6298;&#x534A;&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x6CD5;&#xFF09;&#x5BF9;&#x957F;&#x5EA6;&#x5C0F;&#x4E8E;32&#xFF08;MIN_MERGE&#xFF09;&#x76F4;&#x63A5;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#x8FD4;&#x56DE;&#x7ED3;&#x679C;&#xFF1B;However&#xFF0C;&#x5BF9;&#x4E8E;&#x957F;&#x5EA6;&#x5927;&#x4E8E;&#x7B49;&#x4E8E;32&#x7684;&#x6570;&#x7EC4;&#xFF0C;&#x5148;&#x5206;&#x533A;&#xFF0C;&#x518D;&#x5BF9;&#x5355;&#x4E2A;&#x5206;&#x533A;&#x8FDB;&#x884C;&#x91C7;&#x7528;Binarysort&#x6392;&#x5E8F;&#xFF0C;&#x6700;&#x540E;&#x5408;&#x5E76;&#x5206;&#x533A;&#x5E76;&#x6392;&#x5E8F;&#x3002;</p>
<p><strong>&#x8981;&#x70B9;1&#xFF1A;&#x5206;&#x533A;&#x65B9;&#x6CD5;</strong></p>
<p>&#x5047;&#x5B9A;&#x6570;&#x636E;&#x957F;&#x5EA6;&#x4E3A;n&#xFF0C;
If n &lt; MIN_MERGE(32)&#xFF0C;&#x8FD4;&#x56DE;n
Else if n &#x662F;2&#x7684;&#x500D;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;<code>MIN_MERGE(32) / 2 = 16</code>
Else &#x8FD4;&#x56DE;&#x6574;&#x6570;k&#xFF0C;k&#x53D6;&#x503C;&#x8303;&#x56F4;<code>MIN_MERGE(32) / 2 = 16 &lt;= k &lt;= MIN_MERGE(32)</code></p>
<blockquote>
<p>&#x4F8B;&#xFF1A;
Array Length = 15 ; minRun = 15
Array Length = 50 ; minRun = 25
Array Length = 500 ; minRun = 32</p>
</blockquote>
<p><strong>&#x8981;&#x70B9;2&#xFF1A;&#x5206;&#x533A;&#x6392;&#x5E8F;&#x65B9;&#x6CD5;&#xFF1A;&#x4E8C;&#x5206;&#x6CD5;&#x63D2;&#x5165;&#x6392;&#x5E8F;</strong>&#xFF1A;</p>
<p>1&#x3001; &#x4E8C;&#x5206;&#x6CD5;&#x63D2;&#x5165;&#x6392;&#x5E8F;Binarysort&#x8981;&#x6C42;&#x9996;&#x5148;&#x627E;&#x51FA;&#x6570;&#x7EC4;&#xFF08;&#x6B64;&#x6570;&#x7EC4;&#x5373;&#x5206;&#x533A;&#xFF09;&#x4E2D;&#x4ECE;0&#x4F4D;&#x5F00;&#x59CB;&#x8FDE;&#x7EED;&#x5347;&#x5E8F;&#x533A;&#x5757;&#xFF0C;&#x53CA;&#x533A;&#x5757;&#x4E0B;&#x4E00;&#x4F4D;&#x5143;&#x7D20;pivot&#xFF1B;</p>
<blockquote>
<p>&#x4F8B;&#xFF1A;<code>[1,3,5,7,9,4,8]</code> &#x7684;&#x8D77;&#x59CB;&#x8FDE;&#x7EED;&#x5347;&#x5E8F;&#x533A;&#x5757;&#x662F; <code>[1,3,5,7,9]</code>&#xFF0C;&#x533A;&#x5757;&#x957F;&#x5EA6;&#x4E3A;5&#xFF0C;&#x5373;<strong>runLen</strong>&#xFF1B;pivot&#x662F;<code>4</code>&#xFF1B;</p>
</blockquote>
<p>2&#x3001; &#x901A;&#x8FC7;&#x4E8C;&#x5206;&#x6CD5;&#x6BD4;&#x8F83;pivot&#x4E0E;&#x533A;&#x5206;&#x5143;&#x7D20;&#x7684;&#x5347;&#x964D;&#x5E8F;&#x5173;&#x7CFB;&#xFF0C;&#x8BA1;&#x7B97;pivot&#x5728;&#x533A;&#x5757;&#x4E2D;&#x7684;&#x4F4D;&#x7F6E;&#xFF1B;&#x5E76;&#x63D2;&#x5165;&#x5230;&#x8BE5;&#x4F4D;&#x7F6E;&#xFF0C;&#x7EC4;&#x6210;&#x65B0;&#x7684;&#x533A;&#x5757;&#xFF1B;</p>
<blockquote>
<p>&#x4F8B;&#xFF1A;<code>4</code>&#x5728;&#x533A;&#x5757;&#x4E2D;<code>[1,3,5,7,9]</code>&#xFF0C;&#x5148;&#x6BD4;&#x8F83;<code>5 &gt; 4</code>&#xFF0C;&#x662F;&#x964D;&#x5E8F;&#xFF1B;&#x518D;&#x6BD4;&#x8F83;<code>3 &lt; 4</code>&#xFF0C;&#x662F;&#x5347;&#x5E8F;&#xFF0C;&#x786E;&#x8BA4;&#x4F4D;&#x7F6E;&#xFF0C;&#x901A;&#x8FC7;native&#x65B9;&#x6CD5;<code>System.arraycopy()</code>&#x63D2;&#x5165;&#x5230;&#x533A;&#x5757;&#x4E2D;&#xFF1B;</p>
</blockquote>
<p>3&#x3001; &#x518D;&#x8BA1;&#x7B97;&#x65B0;&#x533A;&#x5757;&#x4E0E;&#x65B0;pivot&#x7684;&#x4F4D;&#x7F6E;&#x5173;&#x7CFB;&#xFF0C;&#x76F4;&#x5230;&#x5B8C;&#x6210;&#x6392;&#x5E8F;&#x3002;</p>
<p>4&#x3001; &#x6CE8;&#x610F;&#xFF1A;&#x4EE5;&#x4E0A;&#x5347;&#x5E8F;&#x7684;&#x5B9A;&#x4E49;&#x662F;<code>Comparator.compare(&#x53F3;&#x5143;&#x7D20;, &#x5DE6;&#x5143;&#x7D20;) &gt;= 0</code>&#xFF0C;&#x53CD;&#x4E4B;&#x4E3A;&#x964D;&#x5E8F;&#xFF1B;&#x975E;&#x6570;&#x503C;&#x4E0A;&#x7684;&#x5347;&#x964D;&#x5E8F;&#x3002;</p>
<h2 id="java-timsort&#x6E90;&#x7801;&#x5206;&#x6790;">Java Timsort&#x6E90;&#x7801;&#x5206;&#x6790;</h2>
<pre><code class="lang-java">    <span class="hljs-keyword">static</span> &lt;T&gt; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">sort</span><span class="hljs-params">(T[] a, <span class="hljs-keyword">int</span> lo, <span class="hljs-keyword">int</span> hi, Comparator&lt;? <span class="hljs-keyword">super</span> T&gt; c,
                         T[] work, <span class="hljs-keyword">int</span> workBase, <span class="hljs-keyword">int</span> workLen)</span> </span>{
        <span class="hljs-keyword">assert</span> c != <span class="hljs-keyword">null</span> &amp;&amp; a != <span class="hljs-keyword">null</span> &amp;&amp; lo &gt;= <span class="hljs-number">0</span> &amp;&amp; lo &lt;= hi &amp;&amp; hi &lt;= a.length;

        <span class="hljs-keyword">int</span> nRemaining  = hi - lo;
        <span class="hljs-keyword">if</span> (nRemaining &lt; <span class="hljs-number">2</span>)
            <span class="hljs-keyword">return</span>;  <span class="hljs-comment">// &#x957F;&#x5EA6;&#x4E3A;0&#x6216;1&#x6570;&#x7EC4;&#x65E0;&#x9700;&#x6392;&#x5E8F;</span>

        <span class="hljs-comment">// &#x6570;&#x7EC4;&#x957F;&#x5EA6;&#x5C0F;&#x4E8E;32&#x65F6;&#xFF0C;&#x76F4;&#x63A5;&#x91C7;&#x7528;binarySort&#x6392;&#x5E8F;&#xFF0C;&#x65E0;&#x9700;&#x5408;&#x5E76;</span>
        <span class="hljs-keyword">if</span> (nRemaining &lt; MIN_MERGE) {
            <span class="hljs-keyword">int</span> initRunLen = countRunAndMakeAscending(a, lo, hi, c);
            binarySort(a, lo, hi, lo + initRunLen, c);
            <span class="hljs-keyword">return</span>;
        }

        TimSort&lt;T&gt; ts = <span class="hljs-keyword">new</span> TimSort&lt;&gt;(a, c, work, workBase, workLen);
        <span class="hljs-comment">// &#x83B7;&#x53D6;&#x5206;&#x533A;&#x957F;&#x5EA6;</span>
        <span class="hljs-keyword">int</span> minRun = minRunLength(nRemaining);
        do {
            <span class="hljs-comment">// &#x8BA1;&#x7B97;&#x76EE;&#x6807;&#x6570;&#x7EC4;&#x6307;&#x5B9A;&#x8303;&#x56F4;&#x4E2D;&#xFF0C;&#x8FDE;&#x7EED;&#x5347;&#x5E8F;&#x6216;&#x8FDE;&#x7EED;&#x964D;&#x5E8F;&#x7684;&#x5143;&#x7D20;&#x7EC4;run&#xFF08;&#x6700;&#x5C11;3&#x4E2A;&#x5143;&#x7D20;&#xFF09;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;run&#x957F;&#x5EA6;</span>
            <span class="hljs-keyword">int</span> runLen = countRunAndMakeAscending(a, lo, hi, c);

            <span class="hljs-comment">// &#x82E5;&#x5206;&#x533A;&#x4E2D;&#x8FDE;&#x7EED;&#x5347;&#x964D;&#x5E8F;&#x7684;&#x5143;&#x7D20;&#x7EC4;&#x957F;&#x5EA6; &#x7B49;&#x4E8E; &#x5206;&#x533A;&#x957F;&#x5EA6;&#xFF0C;&#x5219;&#x65E0;&#x9700;&#x6392;&#x5E8F;&#xFF1B;&#x53CD;&#x4E4B;binarySort&#x91CD;&#x6392;</span>
            <span class="hljs-keyword">if</span> (runLen &lt; minRun) {
                <span class="hljs-keyword">int</span> force = nRemaining &lt;= minRun ? nRemaining : minRun;
                binarySort(a, lo, lo + force, lo + runLen, c);
                runLen = force;
            }

            <span class="hljs-comment">// Push run onto pending-run stack, and maybe merge</span>
            ts.pushRun(lo, runLen);
            ts.mergeCollapse();

            <span class="hljs-comment">// Advance to find next run</span>
            lo += runLen;
            nRemaining -= runLen;
        } <span class="hljs-keyword">while</span> (nRemaining != <span class="hljs-number">0</span>);

        <span class="hljs-comment">// Merge all remaining runs to complete sort</span>
        <span class="hljs-keyword">assert</span> lo == hi;
        ts.mergeForceCollapse();
        <span class="hljs-keyword">assert</span> ts.stackSize == <span class="hljs-number">1</span>;
    }
</code></pre>
<h3 id="&#x83B7;&#x53D6;&#x5206;&#x533A;&#x957F;&#x5EA6;--minrunlength">&#x83B7;&#x53D6;&#x5206;&#x533A;&#x957F;&#x5EA6;--minRunLength()</h3>
<p>If n &lt; MIN_MERGE(32)&#xFF0C;&#x8FD4;&#x56DE;n&#xFF08;&#x6570;&#x636E;&#x957F;&#x5EA6;&#xFF09;
Else if n &#x662F;2&#x7684;&#x500D;&#x6570;&#xFF0C;&#x8FD4;&#x56DE;MIN_MERGE(32)/2=16
Else &#x8FD4;&#x56DE;&#x6574;&#x6570;k&#xFF0C;k&#x53D6;&#x503C;&#x8303;&#x56F4;MIN_MERGE/2(16) &lt;= k &lt;= MIN_MERGE(32)&#xFF0C;&#x4E14;such that n/k
 is close to, but strictly less than, an exact power of 2.&#x4E0D;&#x77E5;&#x5982;&#x4F55;&#x7406;&#x89E3;&#xFF1F;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">minRunLength</span><span class="hljs-params">(<span class="hljs-keyword">int</span> n)</span> </span>{
        <span class="hljs-keyword">assert</span> n &gt;= <span class="hljs-number">0</span>;
        <span class="hljs-keyword">int</span> r = <span class="hljs-number">0</span>;
        <span class="hljs-keyword">while</span> (n &gt;= MIN_MERGE) {
            r |= (n &amp; <span class="hljs-number">1</span>);
            n &gt;&gt;= <span class="hljs-number">1</span>;
        }
        <span class="hljs-keyword">return</span> n + r;
    }
</code></pre>
<h3 id="&#x4E8C;&#x5206;&#x6CD5;&#x63D2;&#x5165;&#x6392;&#x5E8F;--binarysort">&#x4E8C;&#x5206;&#x6CD5;&#x63D2;&#x5165;&#x6392;&#x5E8F;--binarySort()</h3>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * &#x6298;&#x534A;&#x63D2;&#x5165;&#x6392;&#x5E8F;
 * &#x6392;&#x5E8F;&#x7ED3;&#x679C;&#x4E3A;&#x5347;&#x5E8F;&#xFF0C;Comparator.compare(&#x53F3;, &#x5DE6;) &gt;= 0
 * &#x4F8B;&#x5982;&#xFF1A;&#x5047;&#x8BBE;&#x76EE;&#x6807;&#x6570;&#x7EC4;a&#x957F;&#x5EA6;&#x4E3A;10&#xFF0C;&#x6570;&#x7EC4;&#x5934;3&#x4F4D;&#x5DF2;&#x7ECF;&#x6392;&#x597D;&#x5347;&#x5E8F;&#xFF0C;&#x6240;&#x4EE5;
 * lo = 0
 * hi = 9 + 1
 * start = 3 &#xFF08;0 1 2&#x4F4D;&#x5DF2;&#x6392;&#x5E8F;&#xFF0C;&#x4ECE;&#x7B2C;3&#x4F4D;&#x5F00;&#x59CB;&#x8BA1;&#x7B97;&#xFF09;
 * <span class="hljs-doctag">@param</span> a &#x76EE;&#x6807;&#x6570;&#x7EC4;
 * <span class="hljs-doctag">@param</span> lo &#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x8303;&#x56F4;&#x9996;&#x4E2A;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;
 * <span class="hljs-doctag">@param</span> hi &#x6307;&#x5B9A;&#x6392;&#x5E8F;&#x8303;&#x56F4;&#x6700;&#x540E;&#x5143;&#x7D20;&#x4F4D;&#x7F6E; + 1
 * <span class="hljs-doctag">@param</span> start &#x4ECE;start&#x4F4D;&#x7F6E;&#x5F00;&#x59CB;&#x8BA1;&#x7B97;&#x6392;&#x5E8F;&#xFF0C;&#x5373;lo&#x5230;start&#x90E8;&#x5206;&#x4E0D;&#x6392;&#x5E8F;
 */</span>
<span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> &lt;T&gt; <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">binarySort</span><span class="hljs-params">(T[] a, <span class="hljs-keyword">int</span> lo, <span class="hljs-keyword">int</span> hi, <span class="hljs-keyword">int</span> start,
                                       Comparator&lt;? <span class="hljs-keyword">super</span> T&gt; c)</span> </span>{}
</code></pre>
<p>&#x8BE6;&#x7EC6;&#x53C2;&#x8003;&#xFF1A;<a href="https://www.zybuluo.com/zero1036/note/618425" target="_blank">&#x63D2;&#x5165;&#x6392;&#x5E8F;&#x5206;&#x6790;&#x4E0E;Java&#x5B9E;&#x73B0;</a></p>
<h3 id="&#x8BA1;&#x7B97;&#x5206;&#x533A;&#x4E2D;&#x8FDE;&#x7EED;&#x5347;&#x964D;&#x5E8F;&#x5143;&#x7D20;&#x957F;&#x5EA6;--countrunandmakeascending">&#x8BA1;&#x7B97;&#x5206;&#x533A;&#x4E2D;&#x8FDE;&#x7EED;&#x5347;&#x964D;&#x5E8F;&#x5143;&#x7D20;&#x957F;&#x5EA6;--countRunAndMakeAscending()</h3>
<p>&#x8BA1;&#x7B97;&#x76EE;&#x6807;&#x6570;&#x7EC4;&#x6307;&#x5B9A;&#x8303;&#x56F4;&#x4E2D;&#xFF0C;&#x8FDE;&#x7EED;&#x5347;&#x5E8F;&#x6216;&#x8FDE;&#x7EED;&#x964D;&#x5E8F;&#x7684;&#x5143;&#x7D20;&#x7EC4;run&#xFF08;&#x6700;&#x5C11;3&#x4E2A;&#x5143;&#x7D20;&#xFF09;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;run&#x957F;&#x5EA6;&#xFF0C;&#x82E5;&#x8FDE;&#x7EED;&#x964D;&#x5E8F;run&#xFF0C;&#x5219;&#x91CD;&#x7F6E;&#x5176;&#x4E2D;&#x5143;&#x7D20;&#x4E3A;&#x5347;&#x5E8F;&#xFF08;&#x5373;<code>a[lo] &lt;= a[lo + 1] &lt;= a[lo + 2] &lt;=...</code>&#xFF09;&#xFF1B;
&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;run&#x4E3A;<strong>&#x4E25;&#x683C;&#x964D;&#x5E8F;</strong>&#xFF0C;&#x5373;run&#x4E2D;&#x7684;&#x524D;&#x4E00;&#x5143;&#x7D20;&#x5927;&#x4E8E;&#x540E;&#x4E00;&#x5143;&#x7D20;&#xFF08;<code>a[lo] &gt;  a[lo + 1] &gt;  a[lo + 2] &gt;  ...</code>&#xFF09;&#xFF0C;&#x53EF;&#x4EE5;&#x5143;&#x7D20;&#x7FFB;&#x8F6C;&#x3002;&#x4E25;&#x683C;&#x964D;&#x5E8F;&#x4E3A;<code>&gt;</code>&#xFF0C;<code>&gt;=</code>&#x4E0D;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x5728; <code>&gt;=</code> &#x7684;&#x60C5;&#x51B5;&#x4E0B;&#x8FDB;&#x884C;&#x7FFB;&#x8F6C;&#x8FD9;&#x4E2A;&#x7B97;&#x6CD5;&#x5C31;&#x4E0D;&#x518D;&#x662F;stable&#x3002;</p>
<pre><code class="lang-java">    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> &lt;T&gt; <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">countRunAndMakeAscending</span><span class="hljs-params">(T[] a, <span class="hljs-keyword">int</span> lo, <span class="hljs-keyword">int</span> hi,
                                                    Comparator&lt;? <span class="hljs-keyword">super</span> T&gt; c)</span> </span>{
        <span class="hljs-keyword">assert</span> lo &lt; hi;
        <span class="hljs-keyword">int</span> runHi = lo + <span class="hljs-number">1</span>;
        <span class="hljs-keyword">if</span> (runHi == hi)
            <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;

        <span class="hljs-comment">// &#x8BA1;&#x7B97;&#x8FDE;&#x7EED;&#x5347;&#x5E8F;&#x6216;&#x964D;&#x5E8F;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4F4D;&#x5143;&#x7D20;&#x4F4D;&#x7F6E;&#xFF08;runHi&#xFF09;&#xFF0C;&#x964D;&#x5E8F;&#x5219;&#x901A;&#x8FC7;`reverseRange()`&#x7FFB;&#x8F6C;&#x5143;&#x7D20;&#x4E3A;&#x5347;&#x5E8F;</span>
        <span class="hljs-comment">// &#x9996;&#x5148;&#x6BD4;&#x8F83;&#x7B2C;0&#x4E0E;&#x7B2C;2&#x4F4D;</span>
        <span class="hljs-keyword">if</span> (c.compare(a[runHi++], a[lo]) &lt; <span class="hljs-number">0</span>) { <span class="hljs-comment">// &#x964D;&#x5E8F;</span>
            <span class="hljs-comment">// &#x518D;&#x4ECE;&#x7B2C;1&#x4E0E;&#x7B2C;2&#x4F4D;&#x7684;&#x6BD4;&#x8F83;&#x5F00;&#x59CB;&#xFF0C;&#x4F9D;&#x6B21;&#x6BD4;&#x8F83;n&#x4E0E;n + 1&#x4F4D;&#xFF0C;&#x76F4;&#x5230;&#x6BD4;&#x8F83;&#x4E3A;&#x975E;&#x964D;&#x5E8F;</span>
            <span class="hljs-keyword">while</span> (runHi &lt; hi &amp;&amp; c.compare(a[runHi], a[runHi - <span class="hljs-number">1</span>]) &lt; <span class="hljs-number">0</span>)
                runHi++;

            <span class="hljs-comment">// &#x7FFB;&#x8F6C;&#x6570;&#x7EC4;&#x6307;&#x5B9A;&#x8303;&#x56F4;&#x7684;&#x5143;&#x7D20;&#xFF0C;lo&#xFF1A;&#x4F4D;&#x7F6E;&#xFF0C;runHi&#xFF1A;&#x9AD8;&#x4F4D;&#x4F4D;&#x7F6E;&#xFF0C;&#x5373;&#x8303;&#x56F4;&#x957F;&#x5EA6;</span>
            reverseRange(a, lo, runHi);
        } <span class="hljs-keyword">else</span> {                              <span class="hljs-comment">// &#x5347;&#x5E8F;</span>
            <span class="hljs-comment">// &#x4EE5;&#x4E0A;&#x540C;&#x7406;</span>
            <span class="hljs-keyword">while</span> (runHi &lt; hi &amp;&amp; c.compare(a[runHi], a[runHi - <span class="hljs-number">1</span>]) &gt;= <span class="hljs-number">0</span>)
                runHi++;
        }

        <span class="hljs-keyword">return</span> runHi - lo;
    }
</code></pre>
<h3 id="&#x7FFB;&#x8F6C;&#x6570;&#x7EC4;&#x6307;&#x5B9A;&#x8303;&#x56F4;&#x7684;&#x5143;&#x7D20;--reverserange">&#x7FFB;&#x8F6C;&#x6570;&#x7EC4;&#x6307;&#x5B9A;&#x8303;&#x56F4;&#x7684;&#x5143;&#x7D20;--reverseRange()</h3>
<p>lo&#xFF1A;&#x8D77;&#x59CB;&#x4F4D;&#x7F6E;&#xFF0C;&#x4ECE;0&#x8D77;&#x59CB;
hi&#xFF1A;&#x6307;&#x5B9A;&#x8303;&#x56F4;&#x957F;&#x5EA6;</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">reverseRange</span><span class="hljs-params">(Object[] a, <span class="hljs-keyword">int</span> lo, <span class="hljs-keyword">int</span> hi)</span> </span>{
        hi--;
        <span class="hljs-keyword">while</span> (lo &lt; hi) {
            Object t = a[lo];
            a[lo++] = a[hi];
            a[hi--] = t;
        }
    }
</code></pre>
<h3 id="&#x5408;&#x5E76;">&#x5408;&#x5E76;</h3>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">mergeLo</span><span class="hljs-params">(<span class="hljs-keyword">int</span> base1, <span class="hljs-keyword">int</span> len1, <span class="hljs-keyword">int</span> base2, <span class="hljs-keyword">int</span> len2)</span> </span>{
        <span class="hljs-keyword">assert</span> len1 &gt; <span class="hljs-number">0</span> &amp;&amp; len2 &gt; <span class="hljs-number">0</span> &amp;&amp; base1 + len1 == base2;

        <span class="hljs-comment">// Copy first run into temp array</span>
        T[] a = <span class="hljs-keyword">this</span>.a; <span class="hljs-comment">// For performance</span>
        T[] tmp = ensureCapacity(len1);
        <span class="hljs-keyword">int</span> cursor1 = tmpBase; <span class="hljs-comment">// Indexes into tmp array</span>
        <span class="hljs-keyword">int</span> cursor2 = base2;   <span class="hljs-comment">// Indexes int a</span>
        <span class="hljs-keyword">int</span> dest = base1;      <span class="hljs-comment">// Indexes int a</span>
        System.arraycopy(a, base1, tmp, cursor1, len1);

        <span class="hljs-comment">// Move first element of second run and deal with degenerate cases</span>
        a[dest++] = a[cursor2++];
        <span class="hljs-keyword">if</span> (--len2 == <span class="hljs-number">0</span>) {
            System.arraycopy(tmp, cursor1, a, dest, len1);
            <span class="hljs-keyword">return</span>;
        }
        <span class="hljs-keyword">if</span> (len1 == <span class="hljs-number">1</span>) {
            System.arraycopy(a, cursor2, a, dest, len2);
            a[dest + len2] = tmp[cursor1]; <span class="hljs-comment">// Last elt of run 1 to end of merge</span>
            <span class="hljs-keyword">return</span>;
        }

        Comparator&lt;? <span class="hljs-keyword">super</span> T&gt; c = <span class="hljs-keyword">this</span>.c;  <span class="hljs-comment">// Use local variable for performance</span>
        <span class="hljs-keyword">int</span> minGallop = <span class="hljs-keyword">this</span>.minGallop;    <span class="hljs-comment">//  &quot;    &quot;       &quot;     &quot;      &quot;</span>
    outer:
        <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>) {
            <span class="hljs-keyword">int</span> count1 = <span class="hljs-number">0</span>; <span class="hljs-comment">// run1&#x64CD;&#x4F5C;&#x6B21;&#x6570;</span>
            <span class="hljs-keyword">int</span> count2 = <span class="hljs-number">0</span>; <span class="hljs-comment">// run2&#x64CD;&#x4F5C;&#x6B21;&#x6570;</span>

            <span class="hljs-comment">/*
             * &#x3010;&#x9010;&#x4E00;&#x6BD4;&#x5BF9;&#x3011;
             * run1&#xFF08;tmp&#xFF09;&#x4E0E;run2&#xFF08;a&#x7B2C;2&#x6BB5;&#xFF09;&#x6839;&#x636E;&#x5347;&#x5E8F;&#x9010;&#x4E00;&#x6BD4;&#x8F83;&#xFF0C;&#x5F53;compare()&lt;0&#x65F6;&#xFF0C;&#x5199;&#x5165;&#x5230;&#x76EE;&#x6807;&#x6570;&#x7EC4;a
             * &#x5F53;&#x8FDE;&#x7EED;&#x91C7;&#x7528;run1&#x6216;run2&#x7684;&#x5143;&#x7D20;&#x6B21;&#x6570;&#x8FBE;&#x5230;&#xFF08;&#x7B49;&#x4E8E;&#xFF09;7&#x6B21;&#xFF08;minGallop&#xFF09;&#xFF0C;&#x5219;&#x91C7;&#x7528;&#x65B9;&#x6CD5;
             * &#x4F8B;&#x5982;&#xFF1A;run1 = [1,6,7...] ; run2 = [2,4,8...]
             * &#x7ED3;&#x679C;&#xFF1A;
             * a = [1]
             * a = [1,2]
             * a = [1,2,4]
             * a = [1,2,4,6]
             * ...&#x7C7B;&#x63A8;
             */</span>
            do {
                <span class="hljs-keyword">assert</span> len1 &gt; <span class="hljs-number">1</span> &amp;&amp; len2 &gt; <span class="hljs-number">0</span>;
                <span class="hljs-keyword">if</span> (c.compare(a[cursor2], tmp[cursor1]) &lt; <span class="hljs-number">0</span>) {
                    a[dest++] = a[cursor2++];
                    count2++;
                    count1 = <span class="hljs-number">0</span>;
                    <span class="hljs-keyword">if</span> (--len2 == <span class="hljs-number">0</span>)
                        <span class="hljs-keyword">break</span> outer;
                } <span class="hljs-keyword">else</span> {
                    a[dest++] = tmp[cursor1++];
                    count1++;
                    count2 = <span class="hljs-number">0</span>;
                    <span class="hljs-keyword">if</span> (--len1 == <span class="hljs-number">1</span>)
                        <span class="hljs-keyword">break</span> outer;
                }
            } <span class="hljs-keyword">while</span> ((count1 | count2) &lt; minGallop);

            <span class="hljs-comment">/*
             * One run is winning so consistently that galloping may be a
             * huge win. So try that, and continue galloping until (if ever)
             * neither run appears to be winning consistently anymore.
             */</span>
            do {
                <span class="hljs-keyword">assert</span> len1 &gt; <span class="hljs-number">1</span> &amp;&amp; len2 &gt; <span class="hljs-number">0</span>;
                count1 = gallopRight(a[cursor2], tmp, cursor1, len1, <span class="hljs-number">0</span>, c);
                <span class="hljs-keyword">if</span> (count1 != <span class="hljs-number">0</span>) {
                    System.arraycopy(tmp, cursor1, a, dest, count1);
                    dest += count1;
                    cursor1 += count1;
                    len1 -= count1;
                    <span class="hljs-keyword">if</span> (len1 &lt;= <span class="hljs-number">1</span>) <span class="hljs-comment">// len1 == 1 || len1 == 0</span>
                        <span class="hljs-keyword">break</span> outer;
                }
                a[dest++] = a[cursor2++];
                <span class="hljs-keyword">if</span> (--len2 == <span class="hljs-number">0</span>)
                    <span class="hljs-keyword">break</span> outer;

                count2 = gallopLeft(tmp[cursor1], a, cursor2, len2, <span class="hljs-number">0</span>, c);
                <span class="hljs-keyword">if</span> (count2 != <span class="hljs-number">0</span>) {
                    System.arraycopy(a, cursor2, a, dest, count2);
                    dest += count2;
                    cursor2 += count2;
                    len2 -= count2;
                    <span class="hljs-keyword">if</span> (len2 == <span class="hljs-number">0</span>)
                        <span class="hljs-keyword">break</span> outer;
                }
                a[dest++] = tmp[cursor1++];
                <span class="hljs-keyword">if</span> (--len1 == <span class="hljs-number">1</span>)
                    <span class="hljs-keyword">break</span> outer;
                minGallop--;
            } <span class="hljs-keyword">while</span> (count1 &gt;= MIN_GALLOP | count2 &gt;= MIN_GALLOP);
            <span class="hljs-keyword">if</span> (minGallop &lt; <span class="hljs-number">0</span>)
                minGallop = <span class="hljs-number">0</span>;
            minGallop += <span class="hljs-number">2</span>;  <span class="hljs-comment">// Penalize for leaving gallop mode</span>
        }  <span class="hljs-comment">// End of &quot;outer&quot; loop</span>
        <span class="hljs-keyword">this</span>.minGallop = minGallop &lt; <span class="hljs-number">1</span> ? <span class="hljs-number">1</span> : minGallop;  <span class="hljs-comment">// Write back to field</span>

        <span class="hljs-keyword">if</span> (len1 == <span class="hljs-number">1</span>) {
            <span class="hljs-keyword">assert</span> len2 &gt; <span class="hljs-number">0</span>;
            System.arraycopy(a, cursor2, a, dest, len2);
            a[dest + len2] = tmp[cursor1]; <span class="hljs-comment">//  Last elt of run 1 to end of merge</span>
        } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (len1 == <span class="hljs-number">0</span>) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(
                <span class="hljs-string">&quot;Comparison method violates its general contract!&quot;</span>);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">assert</span> len2 == <span class="hljs-number">0</span>;
            <span class="hljs-keyword">assert</span> len1 &gt; <span class="hljs-number">1</span>;
            System.arraycopy(tmp, cursor1, a, dest, len1);
        }
    }
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; tgor.top 2019 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x8BE5;&#x6587;&#x4EF6;&#x4FEE;&#x8BA2;&#x65F6;&#x95F4;&#xFF1A;
2020-04-23 15:32:53
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Java注解.html" class="navigation navigation-prev " aria-label="Previous page: Java注解">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="插入排序分析与Java实现.html" class="navigation navigation-next " aria-label="Next page: 插入排序分析与Java实现">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Timsort排序算法","level":"1.10.8","depth":2,"next":{"title":"插入排序分析与Java实现","level":"1.10.9","depth":2,"path":"Java-Base/插入排序分析与Java实现.md","ref":"Java-Base/插入排序分析与Java实现.md","articles":[]},"previous":{"title":"Java注解","level":"1.10.7","depth":2,"path":"Java-Base/Java注解.md","ref":"Java-Base/Java注解.md","articles":[]},"dir":"ltr"},"config":{"plugins":["chapter-fold","splitter","tbfed-pagefooter","page-toc-button","-search","search-pro","popup","back-to-top-button","code","copy-code-button"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy tgor.top 2019","modify_label":"该文件修订时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"splitter":{},"search-pro":{"cutWordLib":"nodejieba","defineWord":["Gitbook Use"]},"popup":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"code":{"copyButtons":true},"fontsettings":{"theme":"sepia","family":"serif","size":1},"highlight":{},"page-toc-button":{"maxTocDepth":2,"minTocSize":2},"back-to-top-button":{},"copy-code-button":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"tgor","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"tgor note","gitbook":"*","description":"tgor生活工作记录"},"file":{"path":"Java-Base/Timsort排序算法.md","mtime":"2020-04-23T07:32:53.271Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-04-29T08:43:24.127Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-copy-code-button/toggle.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

